<template>
	<view class="shopDetails">
		<!-- <view class="top_box">
			<image class="icon-32" src="../../static/images/icon/icon_back_left.png" mode=""></image>
			<view class="fn-sz-34 fn-cl-333333">商品详情</view>
			<image class="icon-32" src="../../static/images/icon/icon_search.png" mode=""></image>
		</view> -->
		<view class="carousel_box">
			<Carousel :val="bannerObj"></Carousel>
		</view>
		<view class="content">
			<view class="goods_info_box">
				<view class="fn-sz-30 fn-cl-333333 mar-bottom-30">{{ product.name }}</view>
				<view class="goods_sale_info">
					<view class="goods_sale_price_info">
						<view class="now_price fn-cl-F34035">
							<span class="fn-sz-22">￥</span>
							<span class="fn-sz-34" v-if="Number(product.new_price)">{{ product.new_price }}</span>
							<span class="fn-sz-34" v-else>{{ product.price }}</span>
						</view>
						<view class="old_price fn-cl-999999">
							<span class="fn-sz-22">￥</span>
							<span class="fn-sz-34">{{ product.old_price }}</span>
						</view>
					</view>
					<view class="goods_sale_num">
						<span class="fn-sz-24 fn-cl-999999">评分:{{ product.star }}</span>
					</view>
					<view class="goods_sale_num">
						<span class="fn-sz-24 fn-cl-999999">销量:{{ product.sale_num }}</span>
					</view>
				</view>
			</view>
			<view class="coupon_box">
				<view class="user_op_row_box">
					<view class="user_op_row_left_box">
						<view class="op_title">优惠券</view>
						<view class="coupon_box_list">
							<view class="coupon_item" v-for="(item, index) in ticket" :key="index">
								满{{ item.full_use }}减{{ item.price }}</view>
						</view>
					</view>
					<image @tap="couponsOpen" class="icon-30" src="../../static/images/icon/icon_moer_btn.png" mode="">
					</image>
				</view>
				<view class="user_op_row_box">
					<view class="user_op_row_left_box">
						<view class="op_title">自提点</view>
						<view class="user_op_row_box_address more-text">{{ curAddressGetPoint.name }}</view>
					</view>
				</view>
			</view>
			<view class="open_save_card_box">
				<view class="open_save_card_left_box">
					<span class="fn-cl-333333 mar-right-10">开通省钱卡</span>
					<span class="fn-cl-666666">（开通即可省钱）</span>
				</view>
				<view class="open_save_card_right_box" @tap="goLink('/pages/mine/saveMoney')">
					<span>立即开通</span>
					<image class="icon-30" src="../../static/images/icon/icon_moer_btn.png" mode=""></image>
				</view>
			</view>
			<view class="goods_info_safeguard">
				<view class="safeguard_item" v-for="(item, index) in product.team_service" :key="index">
					<image class="icon-28 mar-right-10" src="../../static/images/icon/icon_check_raido_gary.png"
						mode=""></image>
					<span>{{ item }}</span>
				</view>
			</view>
			<view class="goods_address">
				<view class="goods_address_item">
					<view class="fn-sz-26 fn-cl-666666 mar-bottom-10">产地</view>
					<view class="fn-sz-30 fn-cl-222222">{{ product.produce_area }}</view>
				</view>
				<view class="goods_address_item">
					<view class="fn-sz-26 fn-cl-666666 mar-bottom-10">保质期</view>
					<view class="fn-sz-30 fn-cl-222222">{{ product.keep_time }}</view>
				</view>
				<view class="goods_address_item">
					<view class="fn-sz-26 fn-cl-666666 mar-bottom-10">生产日期</view>
					<view class="fn-sz-30 fn-cl-222222">{{ product.produce_time }}</view>
				</view>
			</view>
			<view class="goods_content_box">
				<view class="goods_tab">
					<view :class="['goods_tab_item', curTabs == 0 ? 'active_item' : '']" @tap="changeCurTab(0)">商品详情
					</view>
					<view :class="['goods_tab_item', curTabs == 1 ? 'active_item' : '']" @tap="changeCurTab(1)">用户评价
					</view>
				</view>
				<view class="goods_content_img" v-show="curTabs == 0"><u-parse
						:content="product.content_text"></u-parse></view>
				<view class="rate_list_box" v-show="curTabs == 1">
					<view class="rate_item" v-for="(item, index) in comment_list" :key="index">
						<EvaluationCom :itemData="item"></EvaluationCom>
					</view>
					<view class="check_all_comment" @tap="goLinkComment">
						<span class="mar-right-8">查看全部评价</span>
						<image class="icon-26" src="../../static/images/icon/icon_moer_btn_orange.png" mode=""></image>
					</view>
				</view>
			</view>
			<view class="correlation_recommend" v-if="goodsList.lists.length">
				<view class="fn-sz-32 fn-cl-333333 mar-bottom-30">相关推荐</view>
				<view class="recommend_goods_list">
					<GoodsList :list="goodsList.lists"></GoodsList>
					<u-loadmore :status="loadMoreStatus" v-if="goodsList.lists.length" />
				</view>
			</view>
		</view>

		<view class="bottom_op_box">
			<view class="shoping_car_image" @tap="openShopingCar">
				<image class="icon-70" src="../../static/images/icon/icon_shoping_car_big.png" mode=""></image>
				<view class="shoping_car_num">{{ totalGoodsNum }}</view>
			</view>
			<button class="add_car_btn" @tap="addCart">加入购物车</button>
			<button class="buy_now" @tap="buyNow">立即购买</button>
		</view>

		<!-- 优惠券弹窗 -->
		<u-popup :show="couponsShow" mode="bottom" @close="couponsClose">
			<view class="couponsModel">
				<view class="coupons_model_top">
					<view class="fn-sz-28 fn-cl-222222">优惠券</view>
					<image @tap="couponsClose" class="icon-36" src="../../static/images/icon/icon_gary_radios_fill.png"
						mode=""></image>
				</view>
				<scroll-view scroll-y scroll-with-animation class="coupons_scroll_box">
					<view class="coupons_list">
						<view class="coupons_item" v-for="(item, index) in ticket" :key="index"
							@tap="getTicket(item.id)">
							<view class="coupons_left_price_box fn-cl-F34035">
								<view class="">
									<span class="fn-sz-26">￥</span>
									<span class="fn-sz-52">{{ numberToFix(item.price) }}</span>
								</view>
								<view class="fn-sz-20">满{{ item.full_use }}可用</view>
							</view>
							<view class="coupons_right_date_box">
								<view class="fn-sz-28 mar-bottom-10">有效期</view>
								<view class="fn-sz-24 fn-cl-999999">{{ item.start_date }}至{{ item.end_date }}</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</u-popup>

		<!-- 购买模态框 -->
		<buyGoodsPopup :goodsInfo="product" :popupType="popupType" ref="buyGoodsPopup"></buyGoodsPopup>
		<!-- 购物车模态框 -->
		<ShopingCar ref="shopingCar"></ShopingCar>
		<CreateImage ref="createImage" :goods_image="product.images[0]" :goods_price="product.price"
			@changeShareImage="changeShareImage" v-if="showCreateImage" />
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
	import Carousel from '../../components/carousel/Carousel.vue';
	import GoodsList from '../../components/goodsList/goodsList.vue';
	import EvaluationCom from '../../components/EvaluationCom/EvaluationCom.vue';
	import com from '../../mixin/index.js';
	import buyGoodsPopup from '../../components/buyGoodsPopup/buyGoodsPopup.vue';
	import ShopingCar from '../../components/shopingCar/shopingCar.vue';
	import share from '../../common/js/share.mixin.js';
	import CreateImage from '../../components/createImage.vue'
	export default {
		components: {
			Carousel,
			GoodsList,
			EvaluationCom,
			buyGoodsPopup,
			ShopingCar,
			CreateImage
		},
		mixins: [com, share],
		data() {
			return {
				safeguardList: [],
				bannerObj: {
					list: [],
					height: '750rpx',
					radius: '0rpx'
				},
				curTabs: 0,
				couponsShow: false,
				showCreateImage: false,
				product_id: '',
				product: {},
				recommend: [],
				ticket: [],
				comment_list: [],
				popupType: 'buy',
				shopingCarList: [],
				share: {
					title: '',
					path: '',
					imageUrl: '',
					desc: ''
				}
			};
		},

		onLoad(val) {
			console.log(val, '获取商品id');
			if (val.product_id) {
				this.product_id = val.product_id;
				if (val.addressId) {
					console.log(this.curAddressGetPoint, 'wawawa');
					if (this.curAddressGetPoint) {
						this.curAddressGetPoint.id = val.addressId;
					} else {
						this.curAddressGetPoint = {
							id: val.addressId
						};
					}

					console.log(this.curAddressGetPoint, '2222222');
				}
				if (val.p_mid) {
					uni.setStorageSync('p_mid', val.p_mid);
				}
				this.share.path =
					`/pages/shop/shopDetails?product_id=${val.product_id}&addressId=${this.curAddressGetPoint.id}`;
				this.getDetails();
				this.getShopComment();
				this.shopingCarList = uni.getStorageSync('shoping_car_list');
			}
		},
		onShow() {

		},
		onReachBottom() {
			this.getTeamRecommendList();
		},
		methods: {
			//关闭优惠券模态框
			couponsClose() {
				console.log('关闭咯');
				this.couponsShow = false;
			},
			//开启
			couponsOpen() {
				console.log('开启咯');
				this.couponsShow = true;
			},
			changeCurTab(val) {
				this.curTabs = val;
			},
			changeShareImage(image) {
				this.share.imageUrl = image;
				// this.showShareBtn = true;
				console.log('canvas获取的图片', this.share.imageUrl);
				uni.hideLoading()
			},
			getDetails() {
				this.$api.getProductDetail({
					team_id: this.curAddressGetPoint.id,
					product_id: this.product_id
				}).then(res => {
					console.log(res, '获取商品详情');
					if (res.code != 1) {
						this.$helper.toast(res.msg);
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					}
					const {
						product,
						recommend,
						ticket
					} = res.data;
					this.product = product;
					this.ticket = ticket.map(item => {
						return {
							...item,
							price: parseInt(item.price),
							full_use: parseInt(item.full_use),
							start_date: this.$helper.formatDate(item.start_date, 'yyyy.MM.dd'),
							end_date: this.$helper.formatDate(item.end_date, 'yyyy.MM.dd')
						};
					});
					this.share.imageUrl = this.product.images[0];
					this.share.title = this.product.name;
					this.bannerObj.list = this.product.images;
					this.getTeamRecommendList();
					setTimeout(() => {
						this.showCreateImage = true;
						setTimeout(() => {
							uni.showLoading({
								title: '加载中'
							});
							this.$refs.createImage.getPropsIamge();
							console.log('发送海边IG编外哦', this.$refs);
						}, 10)
					}, 10)
				});
			},
			getTeamRecommendList() {
				let params = {
					product_id: this.product_id,
					team_id: this.curAddressGetPoint.id,
					team_category_id: this.product.team_category_id,
					...this.page
				}
				this.getList('goodsList', 'getTeamRecommend', 'page', params).then(res => {
					console.log(this.goodsList, '当前数据');
				});
			},

			getShopComment() {
				this.$api.getShopComment({
					page: 1,
					page_size: 3,
					team_product_id: this.product_id
				}).then(res => {
					console.log(res, '商品评价0');
					this.comment_list = res.data.lists;
				});
			},
			goLinkComment() {
				uni.navigateTo({
					url: `/pages/shop/allComment?product_id=${this.product_id}`
				});
			},
			getTicket(id) {
				this.$api.getTicket({
					ids: id
				}).then(res => {
					console.log(res, '获取优惠券');
					if (res.code == 1) {
						this.ticket = this.ticket.filter(item => item.id != id);
						this.$helper.toast(res.msg);
					}
				});
			},
			addCart() {
				this.popupType = 'addCar';
				this.$refs.buyGoodsPopup.buyGoodsShow = true;
			},

			buyNow() {
				if (this.product.stock == 0) {
					this.$helper.toast('商品已售完');
					return;
				}
				this.popupType = 'buy';
				this.$refs.buyGoodsPopup.buyGoodsShow = true;
			},
			openShopingCar() {
				if (this.totalGoodsNum) {
					this.$refs.shopingCar.shopingCarShow = true;
				} else {
					this.$helper.toast('购物车是空的!');
				}
			}
		},
		computed: {
			...mapGetters(['totalGoodsNum'])
		}
	};
</script>

<style lang="scss">
	.shopDetails {
		background-color: #f9f9f9;
		padding-bottom: 120rpx;

		.top_box {
			padding: 20rpx 24rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.carousel_box {
			margin-bottom: 20rpx;
		}

		.content {
			padding: 0 24rpx;
		}

		.goods_info_box {
			background: #ffffff;
			border-radius: 20rpx;
			padding: 24rpx 22rpx;
			margin-bottom: 20rpx;

			.goods_sale_info {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.goods_sale_price_info {
					display: flex;
					align-items: center;

					.now_price {
						margin-right: 20rpx;
					}

					.old_price {
						text-decoration: line-through;
					}
				}

				.goods_sale_num {}
			}
		}

		.coupon_box {
			padding: 24rpx 22rpx;
			background: #ffffff;
			margin-bottom: 20rpx;
			border-radius: 20rpx;

			.user_op_row_box {
				display: flex;
				align-items: center;
				justify-content: space-between;

				&:nth-of-type(1) {
					margin-bottom: 40rpx;
				}

				.user_op_row_left_box {
					display: flex;
					align-items: center;
					width: 90%;

					.op_title {
						font-size: 28rpx;
						color: #999999;
						margin-right: 45rpx;
						flex: none;
					}

					.coupon_box_list {
						display: flex;
						align-items: center;
						flex-wrap: wrap;

						.coupon_item {
							padding: 3rpx 7rpx;
							border-radius: 5rpx;
							margin-right: 26rpx;
							margin-bottom: 10rpx;
							color: #ff5b25;
							font-size: 20rpx;
							border: 1px solid #ff4b10;
						}
					}

					.user_op_row_box_address {
						font-size: 28rpx;
						color: #333333;
					}
				}

				image {
					flex: none;
				}

				.user_op_row_right_box {}
			}
		}

		.open_save_card_box {
			padding: 24rpx 15rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 28rpx;
			margin-bottom: 20rpx;
			background-color: #ffffff;
			border-radius: 20rpx;

			.open_save_card_left_box {}

			.open_save_card_right_box {
				display: flex;
				align-items: center;

				span {
					font-size: 28rpx;
					font-weight: 400;
					color: #ff7345;
					margin-right: 10rpx;
				}
			}
		}

		.goods_info_safeguard {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			margin-bottom: 20rpx;
			border-radius: 20rpx;
			background-color: #ffffff;
			padding: 24rpx 22rpx 10rpx;

			.safeguard_item {
				display: flex;
				align-items: center;
				font-size: 26rpx;
				color: #999999;
				margin-bottom: 16rpx;
				margin-right: 20rpx;
			}
		}

		.goods_address {
			display: flex;
			justify-content: space-between;
			margin-bottom: 40rpx;
			background-color: #ffffff;
			border-radius: 20rpx;
			padding: 24rpx 40rpx;

			.goods_address_item {
				text-align: center;
			}
		}

		.goods_content_box {
			.goods_tab {
				display: flex;
				align-items: center;
				margin-bottom: 40rpx;

				.goods_tab_item {
					margin-right: 60rpx;
				}

				.active_item {
					font-weight: bold;
					position: relative;

					&::after {
						content: "";
						position: absolute;
						bottom: -12rpx;
						left: 0;
						right: 0;
						margin: 0 auto;
						width: 48rpx;
						height: 8rpx;
						background: #fed100;
						border-radius: 5rpx;
					}
				}
			}

			.goods_content_img {
				border-radius: 10rpx;
				margin-bottom: 28rpx;
			}

			.rate_list_box {
				background: #ffffff;
				border-radius: 20rpx;
				padding: 33rpx 26rpx;
				margin-bottom: 42rpx;

				.rate_item {
					margin-bottom: 40rpx;
				}

				.check_all_comment {
					display: flex;
					align-items: center;
					justify-content: center;
					color: #f34035;
					font-size: 28rpx;
					line-height: 26rpx;
				}
			}
		}

		.correlation_recommend {
			margin-bottom: 300rpx;

			.recommend_goods_list {}
		}

		.bottom_op_box {
			display: flex;
			position: relative;
			width: 100vw;
			align-items: center;
			background: #ffffff;
			justify-content: space-between;
			padding: 19rpx 23rpx;
			position: fixed;
			box-sizing: border-box;
			bottom: 0;

			.shoping_car_image {
				margin-right: 20rpx;
				position: relative;

				.shoping_car_num {
					position: absolute;
					right: -8rpx;
					top: -6rpx;
					background-color: #f34035;
					border-radius: 50%;
					color: #fff;
					font-size: 26rpx;
					width: 40rpx;
					text-align: center;
					line-height: 40rpx;
					height: 40rpx;
				}
			}

			.add_car_btn {
				width: 330rpx;
				height: 80rpx;
				background: #ffffff;
				border-radius: 40rpx;
				border: 2rpx solid #cccccc;
				color: #333333;
				font-size: 30rpx;
			}

			.buy_now {
				width: 330rpx;
				height: 80rpx;
				background: #fed100;
				border-radius: 40rpx;
				font-size: 30rpx;
			}
		}

		.couponsModel {
			background: #ffffff;
			border-radius: 20rpx 20rpx 0px 0px;
			padding: 30rpx 20rpx;

			.coupons_model_top {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 25rpx;
			}

			.coupons_scroll_box {
				height: 50vh;
			}

			.coupons_list {
				.coupons_item {
					display: flex;
					width: 100%;
					height: 184rpx;
					align-items: center;
					background: url("../../static/images/icon/bg_coupons.png") no-repeat;
					background-size: 100% 184rpx;
					box-sizing: border-box;
					padding: 33rpx 39rpx;
					margin-bottom: 17rpx;

					.coupons_left_price_box {
						margin-right: 67rpx;
					}

					.coupons_right_date_box {}
				}
			}
		}
	}
</style>